import sqlite3
import configparser
import time
from unihiker import GUI, Audio
from pinpong.board import Board, Pin
from pinpong.extension.unihiker import *
from pinpong.board import Servo
from pinpong.extension.unihiker import button_a, button_b

Board().begin()
gui = GUI()
servo1 = Servo(Pin((Pin.P8)))
servo2 = Servo(Pin((Pin.P16)))
angle1 = 90
angle2 = 90
pin_led = Pin(Pin.P7, Pin.OUT)
pin_led.write_digital(1)

def ServoTest1():
    print("left-right")
    global angle1, servo1
    
    while True:
        # print("hh")
        if button_a.is_pressed():
            if angle1 < 180:
                angle1 += 5
                if angle1 >= 180:
                    angle1 = 180
                servo1.write_angle(angle1)
                print(f"Angle increased to: {angle1}")
                time.sleep(0.1)
                
        if button_b.is_pressed():  # 按键B减少角度
            if angle1 > 0:
                angle1 -= 5
                if angle1 <= 0:
                    angle1 = 0
                servo1.write_angle(angle1)
                print(f"Angle decreased to: {angle1}")
                time.sleep(0.1)  # 防止按键太敏感
                
        if button_a.is_pressed() and button_b.is_pressed():
            time.sleep(1)  # 等待1秒确认是长按
            if button_a.is_pressed() and button_b.is_pressed():
                print("退出左右调节")   
                break
    Main_GUI_Init()


def ServoTest2():
    print("舵机控制")
    global angle2, servo2
    
    while True:
        # print("hh")
        if button_a.is_pressed():
            if angle2 < 180:
                angle2 += 5
                if angle2 >= 180:
                    angle2 = 180
                servo2.write_angle(angle2)
                print(f"Angle increased to: {angle2}")
                time.sleep(0.1)
                
        if button_b.is_pressed():  # 按键B减少角度
            if angle2 > 0:
                angle2 -= 5
                if angle2 <= 0:
                    angle2 = 0
                servo2.write_angle(angle2)
                print(f"Angle decreased to: {angle2}")
                time.sleep(0.1)  # 防止按键太敏感
                
        if button_a.is_pressed() and button_b.is_pressed():
            time.sleep(1)  # 等待1秒确认是长按
            if button_a.is_pressed() and button_b.is_pressed():
                print("退出上下调节")
                break
    Main_GUI_Init()

# def btn_a_rasing_handler(pin):#中断事件回调函数
#     global angle1, servo1
#     if angle1 < 180:
#         angle1 += 5
#     if angle1 >= 180:
#         angle1 = 180
#     servo1.write_angle(angle1)
#     print(f"Angle increased to: {angle1}")
#     time.sleep(0.1)

# def btn_b_falling_handler(pin):#中断事件回调函数、
#     global angle1, servo1
#     if angle1 > 0:
#         angle1 -= 5
#     if angle1 <= 0:
#         angle1 = 0
#     servo1.write_angle(angle1)
#     print(f"Angle decreased to: {angle1}")

# button_a.irq(trigger=Pin.IRQ_RISING, handler = btn_a_rasing_handler)#上升沿触发
# button_b.irq(trigger=Pin.IRQ_RISING, handler = btn_b_falling_handler)#下降沿触发


def Main_GUI_Init():
    global info_text1
    gui.add_button(x=180, y=100, w=100, h=30, text="上下控制", origin='center', onclick=lambda: ServoTest2())
    gui.add_button(x=180, y=160, w=100, h=30, text="左右控制", origin='center', onclick=lambda: ServoTest1())

# def Servo1Test():
#     print("舵机控制")
#     global angle1, servo1
#     while True:
#         if button_a.is_pressed():
#             if angle1 < 180:
#                 angle1 += 5
#                 if angle1 >= 180:
#                     angle1 = 180
#                 servo1.write_angle(angle1)
#                 print(f"Angle increased to: {angle1}")
#                 time.sleep(0.1)
                
#         if button_b.is_pressed():  # 按键B减少角度
#             if angle1 > 0:
#                 angle1 -= 5
#                 if angle1 <= 0:
#                     angle1 = 0
#                 servo1.write_angle(angle1)
#                 print(f"Angle decreased to: {angle1}")
#                 time.sleep(0.1)  # 防止按键太敏感
                
#         if button_a.is_pressed() and button_b.is_pressed():
#             time.sleep(1)  # 等待1秒确认是长按
#             if button_a.is_pressed() and button_b.is_pressed():
#                 print("退出上下调节")
#                 break

if __name__ == "__main__":
    ServoTest1()   
    ServoTest2() 
    # Main_GUI_Init()
    while True:            
        time.sleep(1)

